package com.macro.mall.portal.util;

import org.json.JSONObject;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * 微信工具类
 */
public class SignatureUtils {


    //appid
//    private static final String APPID = "wxe4e2be40e7606eca";
    // 深房金融 appid
    private static final String APPID = "wx59d87a41d72f796b";
    //唯一标识
//    private static final String APPSECRET = "64987d552d17bf51ebac2697e91c70d5";
    //深房金融 AppSecret
    private static final String APPSECRET = "e9590210a539cbc5bc620031b5254592";

    //token请求地址
//    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx59d87a41d72f796b&secret=e9590210a539cbc5bc620031b5254592";
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx59d87a41d72f796b&secret=e9590210a539cbc5bc620031b5254592";

    //存放accessToken
    private static AccessToken at;

    public static void main(String[] args) throws Exception {
        getAccessToken();
    }

    private static void getToken() throws Exception {
//        String url = TOKEN_URL.replace("APPID", APPID).replace("APP_SECRET", APPSECRET);
        JSONObject jsonObject = MyHttpUtil.sendJsonGET(TOKEN_URL);
        String token = jsonObject.getString("access_token");
        int expiresIn = jsonObject.getInt("expires_in");
        at = new AccessToken(token, expiresIn);
    }

    /**
     * 向外暴露token
     *
     * @return
     * @throws Exception
     */
    public static String getAccessToken() throws Exception {
        if (at == null || at.isExpired()) {
            getToken();
        }
        return at.getToken();
    }

//    /**
//     * 验证签名
//     *
//     * @param signature
//     * @param timestamp
//     * @param nonce
//     * @return
//     */
//    public static boolean checkSignature(String signature, String timestamp, String nonce) {
//
//        // 1.将token、timestamp、nonce三个参数进行字典序排序
//        String[] arr = new String[]{TOKEN, timestamp, nonce};
//        Arrays.sort(arr);
//
//        // 2. 将三个参数字符串拼接成一个字符串进行sha1加密
//        StringBuilder content = new StringBuilder();
//        for (int i = 0; i < arr.length; i++) {
//            content.append(arr[i]);
//        }
//        MessageDigest md = null;
//        String tmpStr = null;
//        try {
//            md = MessageDigest.getInstance("SHA-1");
//            // 将三个参数字符串拼接成一个字符串进行sha1加密
//            byte[] digest = md.digest(content.toString().getBytes());
//            tmpStr = byteToStr(digest);
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }

//        content = null;
//        // 3.将sha1加密后的字符串可与signature对比，标识该请求来源于微信
//        Boolean asd = tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
//        return asd;
//    }

    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    private static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    private static String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];
        return new String(tempArr);
    }

    /**
     * SHA1 加密
     *
     * @param decript
     * @return
     */
    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


}
